function excelUtils(){
    async function getAllDataOfSheets(file) {
        return new Promise(function (resolve, reject) {
            const fileReader = new FileReader();
            let excelName = file.name;
            try {
                fileReader.onload = ev => {
                    let execl = {
                        name:excelName,
                        data:{}
                    }
                    let obj={};
                    const data = ev.target.result;
                    const workbook = XLSX.read(data, {
                        type: "binary",
                        cellDates: true
                    });
                    for (let sheet in workbook.Sheets) {
                        //循环读取每个文件
                        obj[sheet] = XLSX.utils.sheet_to_json(workbook.Sheets[sheet]);
                    }
                    execl.data = obj
                    // console.log(obj)
                    resolve(execl);
                };
            } catch (e) {
                console.log(e);
                reject({});
            }
            fileReader.readAsBinaryString(file);
        })
    }

    function exportExcel(fileName, data, columnHeaders, wscols) {
        let sheetNames = [];
        let sheetsList = {};
        const wb = XLSX.utils.book_new();
        for (let key in data) {
            sheetNames.push(key);
            sheetsList[key] = XLSX.utils.aoa_to_sheet(transferData(data[key], columnHeaders[key].prop));
            if (arguments.length == 4) {
                sheetsList[key]["!cols"] = wscols[key];
            }
        }
        wb["SheetNames"] = sheetNames;
        wb["Sheets"] = sheetsList;
        XLSX.writeFile(wb, fileName);
    }

    function transferPropertiesName(data, columnHeaders) {
        let tmpData = {};
        for (let sheetName in columnHeaders) {
            if(typeof data[sheetName]!="undefined"){
                if (data[sheetName].length != 0 ) {
                    let arr = data[sheetName].map(v => {
                        let obj = {};
                        for (let prop in columnHeaders[sheetName].prop) {
                            if (typeof v[columnHeaders[sheetName].prop[prop]] == "undefined") {
                                //如果没填这个字段设置空串，不需要可以注释掉
                                obj[prop] = "";
                            } else {
                                obj[prop] = v[columnHeaders[sheetName].prop[prop]];
                            }
                        }
                        return obj;
                    });
                    tmpData[sheetName] = arr;
                } else {
                    tmpData[sheetName] = [];
                }
            }
        }
        return tmpData;
    }

    function transferData(data, columnHeader) {
        let content = [],
            header = [];
        for (let i in columnHeader) {
            header.push(columnHeader[i]);
        }
        content.push(header);
        data.forEach((item, index) => {
            let arr = [];
            for (let i in columnHeader) {
                arr.push(item[i]);
            }
            content.push(arr);
        });
        return content;
    }


    return {
        getAllDataOfSheets:getAllDataOfSheets,
        exportExcel:exportExcel,
        transferPropertiesName:transferPropertiesName
    }
}
import XLSX from 'xlsx';
export {excelUtils}